<template>
    <el-card class="box-card">
      <el-table
          class="table"
          :data="tableData"
          style="width: 100%;">

        <el-table-column
            fixed
            type="index"
            label="序号"
            width="100">
        </el-table-column>

        <el-table-column
            prop="booksName"
            label="书名"
            width="320">
        </el-table-column>

        <el-table-column
            prop="booksPrice"
            label="单价(元)"
            width="320">
        </el-table-column>

        <el-table-column
            prop="cartNum"
            label="数量"
            width="240">
        </el-table-column>

        <el-table-column
            prop="sumPrice"
            label="总价(元)"
            width="240">
        </el-table-column>

        <el-table-column
            label="操作"
            width="615">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="edit(scope.$index)">编辑</el-button>
              <el-button
                  size="mini"
                  type="warning"
                  @click="pay(scope.$index)">支付</el-button>
              <el-button
                  size="mini"
                  type="danger"
                  @click="del(scope.$index)">删除</el-button>
            </template>
        </el-table-column>
      </el-table>
    </el-card>

</template>
<script>
import Header from "../../components/Header";
import router from "../../router";

export default {
  name: "Cart",
  components: {Header},
  data() {
    return {
      tableData: [{
        cartId: ""
      }]
    }
  },
  methods: {
    checkLogin() {
      if (sessionStorage.getItem("isLogin") === "false") {
        this.$message.error("请先登录")
        router.push("/main")
      }
    },
    getCarts() {
      this.$axios.get("/cart/get", {params: {"user_id": sessionStorage.getItem("user_id")}}).then(res => {
        console.log(res.data.data)
        this.tableData = res.data.data
      })
    },
    edit(index) {
      this.$prompt('请输入修改的数量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({value}) => {
        if (parseInt(value) > 0) {
          let cart = {
            cartId: this.tableData[index].cartId,
            cartNum: parseInt(value)
          }
          this.$axios.post("/cart/update", cart).then(res => {
            console.log(res.data.msg)
            this.getCarts()
          })
        } else if (value <= 0) {
          this.$message.error("非法数据")
        }

      }).catch(() => {
        console.log('取消')
      });
    },
    del(index) {
      this.$confirm('确认删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let cart = {
          cartId:this.tableData[index].cartId
        }
        this.$axios.post("/cart/delete", cart).then(res => {
          if (res.data.code == 0) {
            this.$message.success(res.data.message)
          } else {
            this.$message.error(res.data.message)
          }
        })

      }).catch();
    },
    getAddress() {
      this.$axios.get("/address/get",{params:{address_user_id:sessionStorage.getItem("user_id")}}).then(res => {
        if (res.data.data == null) {
          return false
        } else {
          return true
        }
      })
    },
    pay(index){
      if(sessionStorage.getItem("user_id") == "") {
        this.$message.error("请先登录")
      } else {
        if(!this.getAddress()) {
          let value = this.tableData[index].cartNum
            if (parseInt(value) > 0) {
              let order = {
                orderBookId:this.tableData[index].booksId,
                orderUserId: parseInt(sessionStorage.getItem("user_id")),
                orderBookNumber:parseInt(value)
              }
              this.$axios.post("/order/create",order).then(res => {
                if(res.data.code == 0) {
                  this.$message.success(res.data.message)
                  console.log(res.data)
                } else  {
                  this.$message.error(res.data.message)
                }
              })
            } else if (value <= 0) {
              this.$message.error("非法数据")
            }


        } else {
          this.$message.warning("请先填写收货地址")
        }

      }
    }
  },
  created() {
    this.checkLogin()
    this.getCarts()

  }
}
</script>

<style scoped>
  .table {
  }
</style>